/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package character_8;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InsufficientResourcesException;

/**
 *
 * @author Administrator
 */
public class Character_8 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
//        Scanner scanner = new Scanner(System.in);
//        double sum = 0;
//        int count = 0;
//        while (true) {            
//            try {
//                int num = scanner.nextInt();
//                if (num == 0) {
//                    break;
//                }
//                sum += num;
//                count ++;
//            } catch (Exception e) {
//                System.out.println("非整数" + scanner.next());
//                //e.printStackTrace();
//            }
//        }
//        System.out.println("平均" + sum/count);
//        
//        try {
//            System.in.read();
//        } catch (IOException ex) {
//            Logger.getLogger(Character_8.class.getName()).log(Level.SEVERE, null, ex);
//        } catch(Exception e){//放到子类后面
//            e.printStackTrace();
//        }
//        
        try {
            System.out.println("" + readFile("C:\\ckcore.txt"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Character_8.class.getName()).log(Level.SEVERE, null, ex);
        }
        charge(11);
//        try {
//           c(); 
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        System.out.println(testFinally(true));
        
        try(ResourceSome res = new ResourceSome();
                ResourceOther other = new ResourceOther()
                ){
            res.doSome();
            other.doOther();
        } catch(Exception e){
            e.printStackTrace();
        }
        
        try {
            //int num = Integer.parseInt(args[0]);
            //System.out.println(""+num);
        } catch (NumberFormatException e) {
            System.out.println("输入数字");
        }
        
        Object[] objects = {"Java","521"};
        //Integer num1 = (Integer) objects[1];
        //System.out.println(""+num1);
        
        try {
            int a = Integer.parseInt(args[0]);
            System.out.println(a++);
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            System.out.println("输入数字");
        }

    }
    
    public static String readFile(String name) throws IOException{
        
        try (FileInputStream ab = new FileInputStream(name)){
            
        }
        StringBuilder text = new StringBuilder();
       try (Scanner scanner = new Scanner(new FileInputStream(name))){//尝试关闭资源
            ;
            while (scanner.hasNext()) {                
                text.append(scanner.nextLine()).append('\n');
            }
        } 
       catch (IOException ex) {
            ex.printStackTrace();
            Logger.getLogger(Character_8.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        return text.toString();
        
    }
    
    public static void doSome(String arg) throws FileNotFoundException, EOFException{
        
        try {
            if ("one".equals(arg)) {
                throw new FileNotFoundException();
            } else {
                throw new EOFException();
            }
        } catch (FileNotFoundException e) {
            
            e.printStackTrace();
            throw e;
        } catch (EOFException e){
            e.printStackTrace();
            throw e;
        }
    }

    static void c (){
        
        try {
           b(); 
        } catch (Exception e) {
            e.printStackTrace();
            Throwable t = e.fillInStackTrace();
            throw (NullPointerException)t;//堆栈追踪的起点就是重抛异常的起点
        }
    }

    private static void b() {
        a();
    }
    
    private static String a() {
        
       String text = null;
       return text.toUpperCase();
    }
    
    public static void charge(int money){
        assert money < 0 : "koufushu?";
        checkGreaterThanZero(money);
        cheakBalance(money);
    }

    private static void checkGreaterThanZero(int money) {
        if(money < 0){
            //throw new IllegalArgumentException("扣负数？");
        }
        assert money >= 0 : "koufushu?";
    }

    private static void cheakBalance(int money) {
        if (money > 1) {
            //throw new InsufficientException("钱不够了");
        }
    }
    
    static int testFinally(boolean flag){
    
        try{
            if(flag)
            return 1;
        }finally{
            System.out.println("finally");//执行完才返回
        }
        return 0;
    }
    
}
    class CheckedException extends Exception{
        
    }
    
    class UncheckedException extends RuntimeException {
        
    }

class ResourceSome implements AutoCloseable{
    
    void doSome(){
        System.out.println("做一些事");
    }
    @Override
    public void close() throws Exception{
        System.out.println("资源some被关闭");
    }
}

class ResourceOther implements AutoCloseable{
    
    void doOther(){
        System.out.println("做其他事");
    }
    @Override
    public void close() throws Exception{
        System.out.println("资源other被关闭");
    }
}